package com.design.pattern.flyweight.pool;

import java.util.concurrent.ConcurrentLinkedQueue;

public class ThreadPool {
    private static final ThreadInfo threadInfo;
    private static  ConcurrentLinkedQueue<CustomThread> threads=new ConcurrentLinkedQueue<>();
    static {
        threadInfo=new ThreadInfo(10,2000);
    }
    private static CustomThread add(CustomThread thread){
        if (threads.size()<threadInfo.getMax_active()){
                threads.add(thread);
        }else {
            try {
                Thread.currentThread().join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return thread;
    }
    private static CustomThread get(){
        if (threads.size()==0){
            add(new CustomThread());
        }
        return threads.poll();
    }
    private void release(CustomThread customThread){
        add(customThread);
    }
}
